﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;

using System.Web.UI.WebControls;
using Lib.Utility;
using Module.Core.EntitiesInfo;
using Module.DataAccess;
using Module.EntitiesInfo;


namespace Module.BusinessLogic
{
    public class CMSZonesBL
    {
        /// <summary>
        /// Saves a record to the CMS_Zones table.
        /// </summary>
        public static int Insert(CMSZonesInfo cMSZonesInfo)
        {
            try
            {
                return new CMSZonesDB().Insert(cMSZonesInfo);
            }
            catch (Exception ex)
            {
                throw;
            }

        }

        /// <summary>
        /// Saves a record to the CMS_Zones table.
        /// </summary>
        public static void Update(CMSZonesInfo cMSZonesInfo)
        {
            try
            {
                new CMSZonesDB().Update(cMSZonesInfo);
            }
            catch (Exception ex)
            {
                throw;
            }

        }

        /// <summary>
        /// Delete a record from the CMS_Zones table.
        /// </summary>
        public static void Delete(int zoneID)
        {
            try
            {
                new CMSZonesDB().Delete(zoneID);
            }
            catch (Exception ex)
            {
                throw;
            }

        }
      
        public static List<CMSZonesInfo> Select(int type, string param_1,string param_2)
        {
            try
            {
                return ObjectHelper.FillCollection<CMSZonesInfo>(new CMSZonesDB().Select(type, param_1,param_2));
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public static CMSZonesInfo SelectInfo(int type, string param_1, string param_2)
        {
            try
            {
                return ObjectHelper.FillObject<CMSZonesInfo>(new CMSZonesDB().SelectInfo(type, param_1, param_2));
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public static DataTable GetZoneId(int? type, int para_1, int para_2)
        {
            try
            {
                return new CMSZonesDB().GetZoneId(type,para_1,para_2);
            }
            catch (Exception)
            {

                throw;
            }

        }     

        public static DataSet FillToList(int parentId)
        {
            try
            {
                DataSet ds_source = new DataSet();
                DataSet ds = new DataSet();
                DataTable dt = new DataTable();
                DataRow row = null;
                DataColumn colTitle = new DataColumn("Title", typeof(string));
                dt.Columns.Add(colTitle);
                DataColumn colZoneID = new DataColumn("ZoneID", typeof(string));
                dt.Columns.Add(colZoneID);
                DataColumn colZoneIDLang = new DataColumn("ZoneIDLang", typeof(string));
                dt.Columns.Add(colZoneIDLang);

                DataColumn colPriority = new DataColumn("Priority", typeof(string));
                dt.Columns.Add(colPriority);

                DataColumn colModuleMenu = new DataColumn("ModuleMenu", typeof(string));
                dt.Columns.Add(colModuleMenu);

                DataColumn colParentID = new DataColumn("ParentID", typeof(string));
                dt.Columns.Add(colParentID);

                DataColumn colTrangHienThi = new DataColumn("TrangHienThi", typeof(string));
                dt.Columns.Add(colTrangHienThi);

                List<CMSZonesInfo> lstZones = Select(1, parentId.ToString(),"");
                foreach (CMSZonesInfo zone in lstZones)
                {
                    row = dt.NewRow();
                    ListItem _item = new ListItem();
                    _item = new ListItem(zone.Title, zone.ZoneID.ToString());
                    _item.Attributes.Add("Level", "0");
                    row["Title"] = _item;
                    dt.Rows.Add(row);                    
                    GetSub_Items(_item, ConvertUtility.ToInt32(zone.ZoneIDLang), row, dt);
                    row["Priority"] = zone.Priority;
                    row["ZoneID"] = zone.ZoneID;
                    row["ZoneIDLang"] = zone.ZoneIDLang;
                    row["ModuleMenu"] = zone.ModuleMenu;
                    row["ParentID"] = zone.ParentID;
                    row["TrangHienThi"] = zone.TrangHienThi;

                }
                ds_source.Tables.Add(dt);
                return ds_source;
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        private static void GetSub_Items(ListItem curItem, int ParentID,DataRow row, DataTable dt)
        {
            try
            {
                List<CMSZonesInfo> lstZones = Select(1,ParentID.ToString(),"");
                int _level = ConvertUtility.ToInt32(curItem.Attributes["Level"]);
                _level++;
                foreach (CMSZonesInfo zone in lstZones)
                {
                    row = dt.NewRow();
                    ListItem _item = new ListItem();
                    _item = new ListItem(MiscUtility.StringIndent(_level) + zone.Title.ToString(), zone.ZoneID.ToString());
                    _item.Attributes.Add("Level", _level.ToString());
                    dt.Rows.Add(row);
                    GetSub_Items(_item, ConvertUtility.ToInt32(zone.ZoneIDLang),  row, dt);
                    row["Title"] = _item;
                    row["Priority"] = zone.Priority;
                    row["ZoneID"] = zone.ZoneID;
                    row["ZoneIDLang"] = zone.ZoneIDLang;
                    row["ModuleMenu"] = zone.ModuleMenu;
                    row["ParentID"] = zone.ParentID;
                    row["TrangHienThi"] = zone.TrangHienThi;
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        public static void GetSub(ListControl ctrlList, string selectedValue, int lang, int roleId, int userId)
        {
            try
            {
                ctrlList.Items.Clear();
                ctrlList.Items.Insert(0, new ListItem("--------Lựa chọn danh mục --------", "0"));
                List<CMSZonesInfo> lstZones = Select(1, "","");
                foreach (CMSZonesInfo zone in lstZones)
                {
                    ListItem _item = new ListItem(zone.Title, zone.ZoneID.ToString());
                    _item.Attributes.Add("Level", "0");
                    if (_item.Value == selectedValue) _item.Selected = true;
                    ctrlList.Items.Add(_item);
                    GetSubItems(ctrlList, _item, selectedValue, lang, roleId, userId);
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        private static void GetSubItems(ListControl ctrlList, ListItem curItem, string selectedValue, int lang, int roleId, int userId)
        {

            int zoneId = ConvertUtility.ToInt32(curItem.Value);
            int level = ConvertUtility.ToInt32(curItem.Attributes["Level"]);
            level++;
            List<CMSZonesInfo> _lstZones = Select(1,zoneId.ToString(),"");
            foreach (CMSZonesInfo _zone in _lstZones)
            {
                ListItem _item = new ListItem(MiscUtility.StringIndent(level) + _zone.Title, _zone.ZoneID.ToString());
                _item.Attributes.Add("Level", level.ToString());
                if (_item.Value == selectedValue) _item.Selected = true;
                ctrlList.Items.Add(_item);
                GetSubItems(ctrlList, _item, selectedValue, lang, roleId, userId);
            }

        }
        

        #region Get dữ liệu Bin vào combobox Lấy theo ModuleType
        private static void GetSubItemsModule(ListControl ctrlList, ListItem curItem, string selectedValue)
        {

            int zoneId = int.Parse(curItem.Value);
            int level = int.Parse(curItem.Attributes["Level"]);
            level++;
            List<CMSZonesInfo> _lstZones = Select(2, zoneId.ToString(),"");
            if (_lstZones != null && _lstZones.Count > 0)
            {
                foreach (CMSZonesInfo _zone in _lstZones)     
                {
                    ListItem _item = new ListItem(MiscUtility.StringIndent(level) + _zone.Title, _zone.ZoneID.ToString());
                    _item.Attributes.Add("Level", level.ToString());
                    if (_item.Value == selectedValue) _item.Selected = true;
                    ctrlList.Items.Add(_item);
                    GetSubItemsModule(ctrlList, _item, selectedValue);
                }

            }
        }
        public static void GetDataComboType(ListControl ctrlList, string selectedValue, string moduType)
        {
            try
            {
                ctrlList.Items.Clear();
                ctrlList.Items.Insert(0, new ListItem("----------------------", "0"));

                List<CMSZonesInfo> _lstZones = SelectModule(1, "0", moduType);
                if (_lstZones != null && _lstZones.Count > 0)
                {
                    foreach (CMSZonesInfo _zone in _lstZones)
                    {
                        ListItem _item = new ListItem(_zone.Title, _zone.ZoneID.ToString());
                        _item.Attributes.Add("Level", "0");
                        if (_item.Value == selectedValue) _item.Selected = true;
                        ctrlList.Items.Add(_item);
                        //GetSubItemsModule(ctrlList, _item, selectedValue);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static List<CMSZonesInfo> SelectModule(int? Type, string Para_1, string Para_2)
        {
            try
            {
                List<CMSZonesInfo> info=new List<CMSZonesInfo>();
                info = ObjectHelper.FillCollection<CMSZonesInfo>(new CMSZonesDB().SelectModule(Type,Para_1,Para_2));
                if (info != null)
                    return info;
                return null;

            }
            catch (Exception)
            {
                throw;
            }
        }        
        #endregion

       

        public static int GetZoneId(string zoneUrl)
        {
            try
            {               
                int zoneId = 0;               
                zoneId = new CMSZonesDB().GetZoneId(zoneUrl);                
                return zoneId;
            }
            catch (Exception ex)
            {
                throw;
            }

        }
       
        public static string GetZoneUrl(int zoneId, int lang,string linkUrl)
        {
            string zoneUrl=string.Empty;
            int parentId;
            if (linkUrl.IndexOf("http://") > 0 || linkUrl.IndexOf(".") > 0)
            {
                return linkUrl;
            }
            else
            {
                List <CMSZonesInfo> cmsZonesInfo = Select(2,zoneId.ToString(),"");
                if (cmsZonesInfo != null)
                {
                    parentId = cmsZonesInfo[0].ParentID;
                    if (parentId > 0)
                    {
                        zoneUrl = GetZoneUrl(parentId, lang, linkUrl) + "/" + cmsZonesInfo[0].KeyZone;
                    }
                    else
                    {
                        zoneUrl = "/" + cmsZonesInfo[0].KeyZone;
                    }
                }
            }
            
            return zoneUrl;
        }

        public static void UpdateZoneUrl(int zoneId,string zoneUrl)
        {
            try
            {
                new CMSZonesDB().UpdateZoneUrl(zoneId,zoneUrl);
            }
            catch (Exception ex)
            {
                throw;
            }

        }       
    }
}